package com.faith.timefloating;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import afzkl.development.colorpickerview.dialog.ColorPickerDialog;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.faith.timefloating.adapter.WheelInfinitiveAdapter;
import com.faith.timefloating.myutils.Constants;
import com.faith.timefloating.utils.Common;
import com.faith.timefloating.R;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

public class SettingsActivity extends Activity {

	private kankan.wheel.widget.WheelView fontsize;
	private SharedPreferences shared;
	private int notificationId = 212;
	private TextView fontColorBtn;
	private int new_colour=-1;
	private int new_colourBg=-1;
//	private WheelInfinitiveAdapter sizeAdapter;
	private NumericWheelAdapter numericAdapter;
	private String[] sizeArr;
	private Tracker mTracker;
	private TextView bgColorBtn;
	private CheckBox startOnBoot;
	private CheckBox show_battery;
	

	// private NotificationHelper notiHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		Intent startIntent = new Intent(this, TimeFloatingService.class);
		startIntent.putExtra("centerX", width / 2);
		startIntent.putExtra("centerY", height / 2);
		startService(startIntent);
		setContentView(R.layout.settings_layout);
		initGATracker();
		mTracker.sendView("Settings Screen "+Common.getVersionCode(this));
		shared = getSharedPreferences(Constants.SHARED_NAME, MODE_PRIVATE);
		new_colour = shared.getInt(Constants.FONT_COLOR_KEY, -121432);
		new_colourBg = shared.getInt(Constants.FONT_COLOR_BG_KEY, 0);
		int font_size = shared.getInt(Constants.FONT_SIZE_KEY, 15);
		final String displayMode = shared.getString(Constants.TIME_MODE,
				Constants.MODE.LONG_MODE.toString());
		final CheckBox shortMode = (CheckBox) findViewById(R.id.shortmode);
		final CheckBox longMode = (CheckBox) findViewById(R.id.longmode);
		shortMode
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							longMode.setChecked(false);
						} else {
							longMode.setChecked(true);
						}
					}
				});
		longMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					shortMode.setChecked(false);
				} else {
					shortMode.setChecked(true);
				}
			}
		});
		if (displayMode.equalsIgnoreCase(Constants.MODE.LONG_MODE.toString())) {
			longMode.setChecked(true);
			shortMode.setChecked(false);
		} else {
			longMode.setChecked(false);
			shortMode.setChecked(true);
		}
		fontColorBtn = (TextView) findViewById(R.id.fontcolor);
		if(new_colour!=0){
			fontColorBtn.setBackgroundColor(new_colour);
			fontColorBtn.invalidate();
		}
		
		startOnBoot = (CheckBox)findViewById(R.id.start_on_boot);
		boolean enableOnBoot=shared.getBoolean(Constants.START_ON_BOOT, false);
		startOnBoot.setChecked(enableOnBoot);
		fontColorBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showColorPicker(false);
			}
		});
		
		
		fontsize = (WheelView) findViewById(R.id.fontsize);
		bgColorBtn = (TextView) findViewById(R.id.bg_color);
			bgColorBtn.setBackgroundColor(new_colourBg);
			bgColorBtn.invalidate();
		bgColorBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showColorPicker(true);
			}
		});
		
		show_battery = (CheckBox)findViewById(R.id.show_battery);
		show_battery.setChecked(shared.getBoolean(Constants.SHOW_BATTERY, true));
		sizeArr = Common.getFontSizeArr();
//		sizeAdapter = new WheelInfinitiveAdapter(this, sizeArr);
		numericAdapter = new NumericWheelAdapter(this, 9, 23);
		fontsize.setViewAdapter(numericAdapter);
//		fontsize.setViewAdapter(new NumericWheelAdapter(this, 9, 23));
//		fontsize.setViewAdapter(sizeAdapter);
		fontsize.setCurrentItem(Common.getFontIndex(font_size));
		fontsize.setWheelBackgroundColor(Color.TRANSPARENT);
		fontsize.setLineColor(Color.RED);
		fontsize.setLineHeight(1);
		fontsize.setShadowColor(getResources()
						.getColor(R.color.end_lighter_black),getResources().getColor(R.color.midle_lighter_black),
				getResources().getColor(R.color.lighter_black));
		
		Button close = (Button) findViewById(R.id.close);
		close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				int new_font_size=Integer.parseInt(sizeArr[sizeAdapter.getVituarlPosFromRealPos(fontsize.getCurrentItem())]);
				int new_font_size=Common.getFontSizeFromIndex(fontsize.getCurrentItem());
				String new_mode = shortMode.isChecked() ? Constants.MODE.SHORT_MODE.toString() : Constants.MODE.LONG_MODE.toString();
				shared.edit().putInt(Constants.FONT_COLOR_KEY, new_colour).commit();
				shared.edit().putInt(Constants.FONT_COLOR_BG_KEY, new_colourBg).commit();
				shared.edit().putInt(Constants.FONT_SIZE_KEY, new_font_size).commit();
				shared.edit().putString(Constants.TIME_MODE, new_mode).commit();
				shared.edit().putBoolean(Constants.START_ON_BOOT, startOnBoot.isChecked()).commit();
				shared.edit().putBoolean(Constants.SHOW_BATTERY, show_battery.isChecked()).commit();
				if(startOnBoot.isChecked()){
					mTracker.sendEvent("Button Click "+Common.getVersionCode(SettingsActivity.this), "Click save on Settings enable boot & show battery:" + show_battery.isChecked(), "Click save "+(longMode.isChecked()?" with long time mode":"with short time mode & font size:"+new_font_size), Long.valueOf(1));
				}else{
					mTracker.sendEvent("Button Click "+Common.getVersionCode(SettingsActivity.this), "Click save on Settings disable boot  & show battery:" + show_battery.isChecked(), "Click save "+(longMode.isChecked()?" with long time mode":"with short time mode & font size:"+new_font_size), Long.valueOf(1));
				}
					// refresh view;
					Intent intent = new Intent(SettingsActivity.this,TimeFloatingService.class);
					Bundle bundle = new Bundle();
					bundle.putInt(Constants.FONT_COLOR_KEY, new_colour);
					bundle.putInt(Constants.FONT_COLOR_BG_KEY, new_colourBg);
					bundle.putInt(Constants.FONT_SIZE_KEY, new_font_size);
					bundle.putString(Constants.TIME_MODE, new_mode);
					bundle.putBoolean(Constants.ACTION_REFRESH, true);
					bundle.putBoolean(Constants.SHOW_BATTERY, show_battery.isChecked());
					intent.putExtras(bundle);
					startService(intent);
			}
		});
		
	}
	
	private void initGATracker() {
		GoogleAnalytics mGaInstance = GoogleAnalytics.getInstance(this);
		mTracker = mGaInstance.getTracker(getResources().getString(R.string.ga_trackingId));
	}

	private void showColorPicker(final boolean isbgcolor){
		final ColorPickerDialog colorDialog = new ColorPickerDialog(this,isbgcolor==true?new_colourBg: new_colour);
		colorDialog.setAlphaSliderVisible(true);
		colorDialog.setTitle("Pick a Color!");
		colorDialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(android.R.string.ok), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(!isbgcolor){
					new_colour=colorDialog.getColor();
					fontColorBtn.setBackgroundColor(new_colour);
					fontColorBtn.invalidate();
				}else{
					new_colourBg=colorDialog.getColor();
					bgColorBtn.setBackgroundColor(new_colourBg);
					bgColorBtn.invalidate();
				}
				
			}
		});
		
		colorDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//Nothing to do here.
			}
		});
		
		colorDialog.show();
	}
	
}
